<script type = "text/javascript">
function mypopup(){
 window.open("print.php", "myWindow", "status=0, height=600, width=700, resizable=1, scrollbars=1")
}
</script>
<?php
include_once("mysql_connect.inc.php");
mysql_select_db(VELOCITY);
//=========================================================================================================================================
function listEmployees($sortorder, $value)
{
   global $link;

   $sql  = "select a.SignatureID, a.HostUserID, a.FirstName, a.LastName, a.MiddleName, a.Suffix, b.HostDescription ";
   $sql .= "from Users a, UserCredentials b where a.HostUserID = b.HostUserID";


   if ($sortorder == BYLASTNAME)
   {
      $sql .= " order by a.LastName, a.FirstName, a.LastName";
      print '<font color="black" size=5> List of Employees by Last Name</font><br><br>';
   }
   elseif ($sortorder == BYID)
   {
      $sql .= " order by a.HostUserID";
      print '<font color="black" size=5> List of Employees by ID</font><br><br>';
   }
   else
   {
      $sql .= " order by b.HostDescription";
      print '<font color="black" size=5> List of Employees by Host Description</font><br><br>';
   }

   $result=mysql_query($sql,$link);

   if (!$result)
   {
      die("Mysql List user to DB error...".$sql);
   }

   $formshow  = '<TABLE BORDER="1"><TR>';
   $formshow .= '<TD width="36"><font color="green">ID#</font></TD>';
   $formshow .= '<TD width="36"><font color="green">Employee#</font></TD>';
   $formshow .= '<TD width="270"><font color="green">Employee Name</font></TD>';
   $formshow .= '<TD width="200"><font color="green">Host Description</font></TD></TR>';

   while($data=mysql_fetch_array($result,MYSQL_ASSOC))
   {
      if (strlen($data["Suffix"]) > 0)
      {
         $fullname  = $data["LastName"].' '.$data["Suffix"].', '.$data["FirstName"].' '.$data["MiddleName"];
      }
      else
      {
         $fullname  = $data["LastName"].', '.$data["FirstName"].' '.$data["MiddleName"];
      }

      $formshow .= '<TR><TD><font face="arial" color="black" size=2>'.$data["HostUserID"].'<font></TD>';
	  $formshow .= '<TD><font face="arial" color="black" size=2>'.$data["SignatureID"].'<font></TD>';
      $formshow .= '<TD><font face="arial" color="black" size=2>'.$fullname.'<font></TD>';
      $formshow .= '<TD><font face="arial" color="black" size=2>'.$data["HostDescription"].'<font></TD></TR>';
   }

   $formshow .= '</TABLE>';
   $formshow .= '<BR><BR><FONT color="red">Total No. of Employees Listed: '.mysql_num_rows($result).'</font></br>';
   if($value == 2) {
	      $formshow .= '<table>			
				<tr>
				<td align="right" width=650><font size="2" face="MS Sans Serif"><img src="images/printlogo.png" height="20" width="20"><a style="color:#909090" href="javascript:window.print()">print this page</font></a></></td>
				</tr>
				</table>';}   

   if($value == 1){		  
		  $formshow .= '<table>			
				<tr>
				<td align="right" width=650><font size="2" face="MS Sans Serif"><img src="images/printlogo.png" height="20" width="20"><form><input type="button" onClick="mypopup()" value="Click To Print"></form></></td>
				</tr>
				</table>';}

   print $formshow;
}
//=========================================================================================================================================
function listAllEmployeeActivity($department, $fromdatepicker, $todatepicker)
{
   global $link;
   global $mintime;
   global $maxtime;

   $itservices = array(104,83,81,118,95,96,114,82,89,97,115,101,100,85,98,102,84,
                       79,106,92,90,105,116,77,103,86,93,107,91,94);

   $sql  = "select a.HostUserID from Users a order by a.LastName, a.FirstName, a.MiddleName";

   $result=mysql_query($sql,$link);

   if (!$result)
   {
      die("Mysql List user to DB error...".$sql);
   }

   $header = TRUE;

   while($data=mysql_fetch_array($result,MYSQL_ASSOC))
   {
      if ($department == IT)
      {
         if (in_array($data["HostUserID"], $itservices))
         {
            listEmployeeActivity($data["HostUserID"], $fromdatepicker, $todatepicker, TRUE, $header);
            $header = FALSE;
         }
      }
      else
      {
         if (!in_array($data["HostUserID"], $itservices))
         {
            listEmployeeActivity($data["HostUserID"], $fromdatepicker, $todatepicker, TRUE, $header);
            $header = FALSE;
         }
      }
   }

   $formshow  = '</TABLE>';
   
   print $formshow;
}
//=========================================================================================================================================
function listEmployeeActivity($hostid, $fromdatepicker, $todatepicker, $all, $header)
{
   global $link;
   global $mintime;
   global $maxtime;

   $sql  = "select a.HostUserID, a.FirstName, a.LastName, a.MiddleName, a.Suffix, b.HostDescription, b.HostCardStamp ";
   $sql .= "from Users a, UserCredentials b where a.HostUserID = b.HostUserID and a.HostUserID = '".addslashes($hostid)."'";

   $result=mysql_query($sql,$link);

   if (!$result)
   {
      die("Mysql List user to DB error...".$sql);
   }

   $data=mysql_fetch_array($result,MYSQL_ASSOC);
   //print $sql;
   
   if (strlen($data["Suffix"]) > 0)
   {
      $fullname  = $data["LastName"].' '.$data["Suffix"].', '.$data["FirstName"].' '.$data["MiddleName"];
   }
   else
   {
      $fullname  = $data["LastName"].', '.$data["FirstName"].' '.$data["MiddleName"];
   }

   $hostCardStamp = $data["HostCardStamp"];
   //print "BERVE : " . $hostCardStamp;
   //print "BERVE : " . $data["HostCardStamp"];
   $formshow  = '';

   if (!$all)
   {
      $formshow .= '<font color="black" size=5>'.$fullname.'</font><BR><BR><BR>';
   }

   if ($header)
   {
      $formshow .= '<TABLE BORDER="1" align=center><TR>';

      if ($all)
      {
         $formshow .= '<TD width="300"><font color="green">Employee Name</font></TD>';
      }
      $formshow .= '<TD width="100"><font color="green">Date</font></TD>';
      $formshow .= '<TD width="100"><font color="green">Time In</font></TD>';
      $formshow .= '<TD width="100"><font color="green">Time Out</font></TD>';
      $formshow .= '<TD width="110"><font color="green">Total Time</font></TD></TR>';
   }

   //print $fromdatepicker.' '.$todatepicker;
   $fromdate = convertDatePicker($fromdatepicker,"YYYYMMDD");
   $fromdatebin = strtotime($fromdate);
   
   $todate   = convertDatePicker($todatepicker,"YYYYMMDD");
   $todatebin = strtotime($todate);
   $temp     = '';

   $credentialid = getCredentialIDFromDB($hostid);

   if (strlen($credentialid) > 0)
   {
      for ($i=0; $fromdatebin <= $todatebin; $i++)
      {
         $nextdate  = substr(add_date($fromdate, 1,0,0),0,10);

         getTimeEntryExit($hostid, $credentialid, $fromdate, $nextdate, $hostCardStamp, TRUE);

         if ($all)
         {
            $formshow .= '<TR><TD width="200"><font face="arial" color="green">'.$fullname.'</font></TD>';
            $fullname  = '';
         }
         else
         {
            $formshow .= '<TR>';
         }
         
         $formshow .= '<TD align="center"><font face="arial" color="black" size=2>'.$fromdate.'</font></TD>';
         $formshow .= '<TD align="center"><font face="arial" color="black" size=2>'.$mintime.'</font></TD>';
         $formshow .= '<TD align="center"><font face="arial" color="black" size=2>'.$maxtime.'</font></TD>';

         if ($mintime != NULL)
         { 
            $formshow .= '<TD align="center"><font face="arial" color="black" size=2>'.date_diff1($mintime, $maxtime).'</font></TD></TR>';
         }
         else
         {
            $formshow .= '<TD align="center"><font face="arial" color="black" size=2></font></TD></TR>';
         }

         //print $fromdate;
         $fromdate  = $nextdate;
         $fromdatebin = strtotime($fromdate);
      }
   }

   if (!$all)
   {
      $formshow .= '</TABLE></br>';
	      $formshow .= '<table>			
<tr>
<td align="right" width=650><font size="2" face="MS Sans Serif"><img src="images/printlogo.png" height="20" width="20"><a style="color:#909090" href="javascript:window.print()">print this page</font></a></></td>
</tr>
</table>';
   }
   //$formshow .= '<BR><BR><FONT color="red">Total No. of Employees Listed: '.mysql_num_rows($result).'</font>';
   
   print $formshow;
}
//=========================================================================================================================================
function showEmployeeList($function, $action, $submitname)
{
   global $errmsg;

   $formshow  = '<FORM METHOD="POST" ACTION="'.$action.'">';
   $formshow .= '<font color=red>'.$errmsg.'<br><br><br></font>';
   $formshow .= '<TABLE BORDER="0"><TR>';
   $formshow .= '<TD width="250">Employee</font></TD>';
   $formshow .= '<TD><SELECT name="empid">';
   $formshow .= EmployeeListFromDBToSelect();
   $formshow .= '</SELECT></TD></TR>';
   $formshow .= '<TD>Period</TD>';
   $formshow .= '<TD><input type="Text" id=fromdate name=fromdate maxlength="25" size="10">';
   $formshow .= '<a href="javascript:NewCal(\'fromdate\',\'MMDDYYYY\',false,24)">';
   $formshow .= '<img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>';
   $formshow .= ' - <input type="Text" id=todate name=todate maxlength="25" size="10">';
   $formshow .= '<a href="javascript:NewCal(\'todate\',\'MMDDYYYY\',false,24)">';
   $formshow .= '<img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a></TD></TR>';
   $formshow .= '<TD><TD><font color=red>mm-dd-yy</font>';
   $formshow .= '</TABLE>';
   $formshow .= '<P ALIGN=CENTER><BR><BR><INPUT TYPE="SUBMIT" VALUE="'.$submitname.'" NAME="submit"></P>';
   $formshow .= '</FORM>';

   print $formshow;
}
//=========================================================================================================================================
function showEmployeeList_ID($function, $action, $submitname)
{
   global $errmsg;

   $formshow  = '<FORM METHOD="POST" ACTION="'.$action.'">';
   $formshow .= '<font color=red>'.$errmsg.'<br><br><br></font>';
   $formshow .= '<TABLE BORDER="0"><TR>';
   $formshow .= '<TD width="250">Employee</font></TD>';
   $formshow .= '<TD><SELECT name="empid">';
   $formshow .= EmployeeListFromDBToSelect_ID();
   $formshow .= '</SELECT></TD></TR>';
   $formshow .= '<TD>Period</TD>';
   $formshow .= '<TD><input type="Text" id=fromdate name=fromdate maxlength="25" size="10">';
   $formshow .= '<a href="javascript:NewCal(\'fromdate\',\'MMDDYYYY\',false,24)">';
   $formshow .= '<img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>';
   $formshow .= ' - <input type="Text" id=todate name=todate maxlength="25" size="10">';
   $formshow .= '<a href="javascript:NewCal(\'todate\',\'MMDDYYYY\',false,24)">';
   $formshow .= '<img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a></TD></TR>';
   $formshow .= '<TD><TD><font color=red>mm-dd-yy</font>';
   $formshow .= '</TABLE>';
   $formshow .= '<P ALIGN=CENTER><BR><BR><INPUT TYPE="SUBMIT" VALUE="'.$submitname.'" NAME="submit"></P>';
   $formshow .= '</FORM>';

   print $formshow;
}
//=========================================================================================================================================
function EmployeeListFromDBToSelect_ID()
{
   global $link;

   $sql  = "select a.HostUserID, a.FirstName, a.LastName, a.MiddleName, a.Suffix ";
   $sql .= "from Users a order by a.HostUserID, a.LastName, a.FirstName, a.MiddleName";

   $result=mysql_query($sql,$link);

   if (!$result)
   {
      die("Mysql List user to DB error...".$sql);
   }

   $select = "";

   while($data=mysql_fetch_array($result,MYSQL_ASSOC))
   {
      if (strlen($data["Suffix"]) > 0)
      {
         $fullname  = $data["LastName"].' '.$data["Suffix"].', '.$data["FirstName"].' '.$data["MiddleName"];
      }
      else
      {
         $fullname  = $data["LastName"].', '.$data["FirstName"].' '.$data["MiddleName"];
      }
      
      $select .= '<option value="'.$data["HostUserID"].'">';
      $select .= $data["HostUserID"].' - '.$fullname.'</option>';
   }

   return($select);
}
//=========================================================================================================================================
function EmployeeListFromDBToSelect()
{
   global $link;

   $sql  = "select a.HostUserID, a.FirstName, a.LastName, a.MiddleName, a.Suffix ";
   $sql .= "from Users a order by a.LastName, a.FirstName, a.MiddleName";

   $result=mysql_query($sql,$link);

   if (!$result)
   {
      die("Mysql List user to DB error...".$sql);
   }

   $select = "";

   while($data=mysql_fetch_array($result,MYSQL_ASSOC))
   {
      if (strlen($data["Suffix"]) > 0)
      {
         $fullname  = $data["LastName"].' '.$data["Suffix"].', '.$data["FirstName"].' '.$data["MiddleName"];
      }
      else
      {
         $fullname  = $data["LastName"].', '.$data["FirstName"].' '.$data["MiddleName"];
      }
      
      $select .= '<option value="'.$data["HostUserID"].'">';
      $select .= $fullname.' - '.$data["HostUserID"].'</option>';
   }

   return($select);
}
//=========================================================================================================================================
function getCredentialIDFromDB($hostuserid)
{
   global $link;

   $sql  = "select max(CredentialId) CredentialId from UserCredentials ";
   $sql .= "where HostUserID = ".addslashes($hostuserid);

   $result=mysql_query($sql,$link);

   if (!$result)
   {
      die("Mysql user credentials to DB error...".$sql);
   }

   $data=mysql_fetch_array($result,MYSQL_ASSOC);

   if (mysql_num_rows($result) > 0)
   {
      return($data["CredentialId"]);
   }
   else
   {
      return("");
   }
}
//=========================================================================================================================================
function getTimeEntryExit($hostid, $credentialid, $fromdate, $todate, $hostCardStamp)
{
   global $link;
   global $mintime;
   global $maxtime;

   $itservices = array(104,83,81,118,95,96,114,82,89,97,115,101,100,85,98,102,84,
                       79,99,106,92,90,105,116,77,103,86,93,107,91,94,108,136);

   $sql  = "SELECT DATE_FORMAT(min(dtDate),'%H:%i:%s') TIME_IN ";
   $sql .= "from log_transactions where UID1 = ".$credentialid." ";
   $sql .= "and dtDate >= '".addslashes($fromdate)."' ";
   $sql .= "and dtDate < '".addslashes($todate)."' ";

   //if (!in_array($hostid, $itservices))
   if (substr($hostCardStamp,0,1) == "1" || substr($hostCardStamp,0,1) == " ")
   {
      $sql .= "and (ReaderName = '".addslashes(MAINDOOR1ENTRYA)."'";
      $sql .= " or ReaderName = '".addslashes(MAINDOOR1ENTRYB)."')";
   }
   else
   {
      if (substr($hostCardStamp,0,1) == "2")
      {
         $sql .= "and (ReaderName = '".addslashes(MAINDOOR2ENTRYA)."'";
         $sql .= " or ReaderName = '".addslashes(MAINDOOR2ENTRYB)."')";
      }
      else
      {
         $sql .= "and ReaderName = '".addslashes(MAINDOOR3ENTRY)."'";
         //$sql .= " or ReaderName = '".addslashes(MAINDOOR1ENTRYB)."')";
      }
   }

   //print $hostCardStamp;
   //print $sql;
   
   $result=mysql_query($sql,$link);

   if (!$result)
   {
      die("Mysql user Entry to DB error...".$sql);
   }

   if (mysql_num_rows($result) > 0)
   {
      $data=mysql_fetch_array($result,MYSQL_ASSOC);
      $mintime = $data["TIME_IN"];
      //print $mintime;

      $sql  = "SELECT DATE_FORMAT(max(dtDate),'%H:%i:%s') TIME_OUT ";
      $sql .= "from log_transactions where UID1 = ".$credentialid." ";
      $sql .= "and dtDate >= '".addslashes($fromdate)."' ";
      $sql .= "and dtDate < '".addslashes($todate)."' ";

      //if (!in_array($hostid, $itservices))
      if (substr($hostCardStamp,0,1) == "1" || substr($hostCardStamp,0,1) == " ")
      {
         $sql .= "and (ReaderName = '".addslashes(MAINDOOR1EXITA)."'";
         $sql .= " or ReaderName = '".addslashes(MAINDOOR1EXITB)."')";
      }
      else
      {
         if (substr($hostCardStamp,0,1) == "2")
         {
            $sql .= "and (ReaderName = '".addslashes(MAINDOOR2EXITA)."'";
            $sql .= " or ReaderName = '".addslashes(MAINDOOR2EXITB)."')";
         }
         else
         {
            $sql .= "and ReaderName = '".addslashes(MAINDOOR3EXIT)."'";
            //$sql .= " or ReaderName = '".addslashes(MAINDOOR1EXITB)."')";
         }
      }

      //print $sql;
      $result=mysql_query($sql,$link);

      if (!$result)
      {
         die("Mysql user Exit to DB error...".$sql);
      }

      if (mysql_num_rows($result) > 0)
      {
         $data=mysql_fetch_array($result,MYSQL_ASSOC);
         $maxtime = $data["TIME_OUT"];
         //print $maxtime;
      }
   }  
}
//=========================================================================================================================================
function showDepartmentList()
{
   global $errmsg;

   $formshow  = '<FORM METHOD="POST" ACTION="bydepinout.php">';
   $formshow .= '<font color=red>'.$errmsg.'<br><br><br></font>';
   $formshow .= '<TABLE BORDER="0"><TR>';
   $formshow .= '<TD width="250">Employee</font></TD>';
   $formshow .= '<TD><SELECT name="department">';
   $formshow .= '<option value="'.IT.'">';
   $formshow .= IT.'</option>';
   $formshow .= '<option value="'.BPO.'">';
   $formshow .= BPO.'</option>';
   $formshow .= '</SELECT></TD></TR>';
   $formshow .= '<TD>Period</TD>';
   $formshow .= '<TD><input type="Text" id=fromdate name=fromdate maxlength="25" size="10">';
   $formshow .= '<a href="javascript:NewCal(\'fromdate\',\'MMDDYYYY\',false,24)">';
   $formshow .= '<img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>';
   $formshow .= ' - <input type="Text" id=todate name=todate maxlength="25" size="10">';
   $formshow .= '<a href="javascript:NewCal(\'todate\',\'MMDDYYYY\',false,24)">';
   $formshow .= '<img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a></TD></TR>';
   $formshow .= '<TD><TD><font color=red>mm-dd-yy</font>';
   $formshow .= '</TABLE>';
   $formshow .= '<P ALIGN=CENTER><BR><BR><INPUT TYPE="SUBMIT" VALUE="Continue" NAME="submit"></P>';
   $formshow .= '</FORM>';

   print $formshow;
}
//=========================================================================================================================================
?>
